package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dao.IEmpSocChangesDAO;
import org.jsola.hr.dto.EmpSocChangesAddDTO;
import org.jsola.hr.dto.EmpSocChangesUpdateDTO;
import org.jsola.hr.entity.EmpSocChangesDO;
import org.jsola.hr.query.EmpSocChangesQuery;
import org.jsola.hr.service.IEmpSocChangesService;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.hr.vo.EmpSocChangesVO;
import org.jsola.module.ModuleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static org.jsola.hr.core.AbstractTest.SITE_ID;
import static org.jsola.hr.core.TestCommonConstans.CODE_EMP_LIST;
import static org.jsola.hr.core.TestCommonConstans.EMP_SOC_CHANGES_DO_LIST;


/**
 * @author zhr
 */
@Component
public class EmpSocChangesTestHelper extends BaseTestHelper {

    @Autowired
    private IEmpSocChangesService empSocChangesService;

    @Autowired
    private IEmpSocChangesDAO empSocChangesDAO;

    /**
     * 生成add测试数据
     * @return EmpSocChangesAddDTO
     */
    public EmpSocChangesAddDTO getEmpSocChangesAddDTO(){
        EmpSocChangesAddDTO empSocChangesAddDTO = new EmpSocChangesAddDTO();
        empSocChangesAddDTO.setEmpId(getRandomValue("varchar",32));
        empSocChangesAddDTO.setSocCity(getRandomValue("varchar",32));
        empSocChangesAddDTO.setSocType(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setSocBasic(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setSocPropCompany(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setSocPropPerson(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setSocAmountCompany(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setSocAmountPerson(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setSocRemark(getRandomValue("varchar",255));
        empSocChangesAddDTO.setSocMain(SafeKit.getInteger(getRandomValue("tinyint",4)));
        empSocChangesAddDTO.setCensusType(SafeKit.getInteger(getRandomValue("tinyint",4)));
        empSocChangesAddDTO.setWorkInjuryProp(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setPfCity(getRandomValue("varchar",32));
        empSocChangesAddDTO.setPfBasic(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setPfPropCompany(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setPfPropPerson(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setPfAmountCompany(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setPfAmountPerson(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setPfTaxAmount(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setPfRemark(getRandomValue("varchar",255));
        empSocChangesAddDTO.setPersonTaxProp(SafeKit.getBigDecimal(getRandomValue("decimal",11)));
        empSocChangesAddDTO.setPersonTaxAmount(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setSocUnpayMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setCompanyId(getRandomValue("varchar",32));
        empSocChangesAddDTO.setPfUnpayMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setSocPaybackMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setPfPaybackMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setSocPaybackMonthAmount(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setPfPaybackMonthAmount(SafeKit.getInteger(getRandomValue("int",11)));
        empSocChangesAddDTO.setSocPaybackMonthRemark(getRandomValue("varchar",255));
        empSocChangesAddDTO.setPfPaybackMonthRemark(getRandomValue("varchar",255));
        empSocChangesAddDTO.setEffectiveStatus(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        empSocChangesAddDTO.setEffectiveDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        return empSocChangesAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpSocChangesUpdateDTO
     */
    public EmpSocChangesUpdateDTO getEmpSocChangesUpdateDTO(EmpSocChangesVO empSocChangesVO){
        return empSocChangesService.selectDOById(empSocChangesVO.getId(), SITE_ID).to(EmpSocChangesUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpSocChangesQuery
     */
    public EmpSocChangesQuery getEmpSocChangesQuery() {
        EmpSocChangesQuery empSocChangesQuery = new EmpSocChangesQuery();
        List<EmpSocChangesDO> empSocChangesDOList = empSocChangesService.list(new EmpSocChangesDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empSocChangesDOList)) {
            EmpSocChangesDO empSocChangesDO = empSocChangesDOList.get(0);
            empSocChangesQuery = empSocChangesDO.to(EmpSocChangesQuery.class);
            if (empSocChangesDO.getEffectiveDate() != null) {
                empSocChangesQuery.setEffectiveDateStart(DateKit.getBeforeDay(empSocChangesDO.getEffectiveDate(),-1));
                empSocChangesQuery.setEffectiveDateEnd(DateKit.getBeforeDay(empSocChangesDO.getEffectiveDate(),1));
            }
            if (empSocChangesDO.getGmtCreate() != null) {
                empSocChangesQuery.setGmtCreateStart(DateKit.getBeforeDay(empSocChangesDO.getGmtCreate(),-1));
                empSocChangesQuery.setGmtCreateEnd(DateKit.getBeforeDay(empSocChangesDO.getGmtCreate(),1));
            }
            if (empSocChangesDO.getGmtModified() != null) {
                empSocChangesQuery.setGmtModifiedStart(DateKit.getBeforeDay(empSocChangesDO.getGmtModified(),-1));
                empSocChangesQuery.setGmtModifiedEnd(DateKit.getBeforeDay(empSocChangesDO.getGmtModified(),1));
            }
        }
        return empSocChangesQuery;
    }

    /**
     * 保存示例
     * @return EmpSocChangesVO
     */
    public EmpSocChangesVO saveEmpSocChangesExample(){
        EmpSocChangesAddDTO addDTO = this.getEmpSocChangesAddDTO();
        EmpSocChangesVO empSocChangesVO = empSocChangesService.save(addDTO,buildTokenUser());
        return empSocChangesVO;
    }

    /**
     * 批量生成add测试数据
     * @return EmpSecretKeysAddDTO
     */
    public ModuleContext batchSaveEmpSocExample(ModuleContext moduleContext){
        List<EmpInfoVO> empInfoVOList = moduleContext.get(CODE_EMP_LIST);
        List<EmpSocChangesDO> EmpSocChangesDOList = empInfoVOList.stream().map(empInfoVO -> {
            EmpSocChangesAddDTO empSocChangesAddDTO = new EmpSocChangesAddDTO();
            empSocChangesAddDTO.setEmpId(empInfoVO.getId());
            empSocChangesAddDTO.setSocCity(getRandomValue("varchar",32));
            empSocChangesAddDTO.setSocType(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            empSocChangesAddDTO.setSocBasic(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocPropCompany(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocPropPerson(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocAmountCompany(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocAmountPerson(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocRemark(getRandomValue("varchar",255));
            empSocChangesAddDTO.setSocMain(SafeKit.getInteger(getRandomValue("tinyint",4)));
            empSocChangesAddDTO.setCensusType(SafeKit.getInteger(getRandomValue("tinyint",4)));
            empSocChangesAddDTO.setWorkInjuryProp(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfCity(getRandomValue("varchar",32));
            empSocChangesAddDTO.setPfBasic(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfPropCompany(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfPropPerson(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfAmountCompany(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfAmountPerson(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfTaxAmount(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfRemark(getRandomValue("varchar",255));
            empSocChangesAddDTO.setPersonTaxProp(SafeKit.getBigDecimal(getRandomValue("int",11)));
            empSocChangesAddDTO.setPersonTaxAmount(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setSocUnpayMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            empSocChangesAddDTO.setCompanyId(getRandomValue("varchar",32));
            empSocChangesAddDTO.setPfUnpayMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            empSocChangesAddDTO.setSocPaybackMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            empSocChangesAddDTO.setPfPaybackMonth(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            empSocChangesAddDTO.setSocPaybackMonthAmount(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setPfPaybackMonthAmount(SafeKit.getInteger(getRandomValue("int",11)));
            empSocChangesAddDTO.setEffectiveStatus(true);
            empSocChangesAddDTO.setEffectiveDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
            EmpSocChangesDO empSocChangesDO = empSocChangesAddDTO.to(EmpSocChangesDO.class);
            empSocChangesDO.setValid(true);
            empSocChangesDO.setSiteId(buildTokenUser().getSiteId());
            return empSocChangesDO;
        }).collect(Collectors.toList());

        empSocChangesDAO.insertListAndSetId(EmpSocChangesDOList);
        moduleContext.add(EMP_SOC_CHANGES_DO_LIST, EmpSocChangesDOList);
        return moduleContext;
    }
}
